<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class PinpaiController extends BaseController
{
    //显示品牌列表
    public function getIndex(Request $request)
    {
         //获取搜索框内容
        $search=$request->input('keyword');
        $num=$request->input('pagenum',2);

        $data=DB::table('pinpai')->where('pp_name','like','%'.$search.'%')->paginate($num);

        return view('admin.pinpai.index',['data'=>$data,'requset'=>$request]);

    }

    //显示添加页面
    public function getAdd()
    {
        return view('admin.pinpai.add');
    }

    //执行添加方法
    public function postInsert(Request $request)
    {
        $data = $request->except('_token');

        $pp_logo=$this->UploadOneImage('pp_logo','pp_logo');
        $data['pp_logo']=$pp_logo;
        $data['pp_time']=time();

        //上传数据
        $z=DB::table('pinpai')->insert($data);
        if($z){
            return redirect('admin/pinpai/index')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }

    }

    //显示修改页面
    public function getEdit($id)
    {
        $data=DB::table('pinpai')->where('pp_id',$id)->first();

        return view('admin.pinpai.edit',['data'=>$data]);
    }

    //执行修改方法
    public function postUpdata(Request $request,$id)
    {

        //过滤字段
        $data=$request->except('_token');
        //验证输入信息
        $this->validate($request,[
            'pp_name'=>'required',
            'pp_url'=>'required ',

        ],[
            'pp_name.required'=>'品牌名不能为空',
            'adm_url.required'=>'品牌名不能为空',
        ]);

        //调用上传单张图片的方法
        $adm_face=$this->UploadOneImage('pp_logo','pp_logo');
        $data['pp_logo']=$adm_face;
        //插入数据
        $z=DB::table('pinpai')->where('pp_id',$id)->update($data);
        if($z){
            return redirect('admin/pinpai/index')->with('successs','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    //执行删除方法
    public function getDelete($id)
    {
        $z=DB::table('pinpai')->where('pp_id',$id)->delete();
        if($z){

            $data=[
                'status'=>0,
            ];
        }else{
            $data=[
                'status'=>1,
            ];
        }

        return $data;
    }
}
